// 网关配置的前缀
var blogServer = "/blogSer"
var number = 4;

var currentPage = 1;    // 当前页
var pageSize = 10;      // 页大小
var total = 0;          // 数据总量
var pages = 1;          // 页数总量

let searchValue = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        blogServer = data.blogServer;
    });
    getBlogInfo(searchValue, currentPage, pageSize);
    getHotBlog();
}

function getBlogInfo(searchValue, currentPage, pageSize) {
    $.ajax({
        type: "get",
        async: false,
        url: blogServer + "/blog/blogInfo",
        data: {
            searchValue: searchValue,
            currentPage: currentPage,
            pageSize: pageSize
        },
        success: function (data) {
            console.log("所有的博客数据 - 第 " + currentPage + " 页");
            total = data.datas.total;
            pageSize = data.datas.size;
            pages = data.datas.pages;
            //清空数据
            $("#blogContent").empty();
            //返回的数据用data.rows获取内容
            $.each(data.datas.records, function (i, item) {

                let content = "";

                if (item.blogContent === null) {
                    content = "暂无内容";
                }else {
                    let reg = /<[^>]*>/g
                    content = item.blogContent.replace(reg,'')
                }
                $("#blogContent").append(
                    "<div class='col-12 blog-area'>" +
                    "<div class='single-blog-post mb-90 mb-xs-50'>" +
                    "<div class='blog-content'>" +
                    "<h4 class='title'>" +
                    "<a  onclick='blogDetail(\""+item.blogId+"\")' >" + item.blogTitle + "</a>" +
                    "</h4>" +
                    "<ul class='post-meta'>" +
                    "<li><i class='lnr lnr-user'></i><a>" + item.userName + "</a></li>" +
                    "<li><i class='lnr lnr-eye'></i><a>" + item.readNumber + "</a></li>" +
                    "<li><i class='lnr lnr-clock'></i><a> " + item.createTime + "</a></li>" +
                    "</ul>" +
                    "<p class='blog-desc'>" + content.substring(0, 20) + " ……" + "</p>" +
                    "<a onclick='blogDetail(\""+item.blogId+"\")' class='ht-btn theme-btn theme-btn-two'>View more " +
                    "<i class='lnr lnr-chevron-right'></i>" +
                    "</a>" +
                    "</div>" +
                    "</div>" +
                    "</div>"
                );

            });

            // pageMe.js 使用方法
            $("#page").paging({
                pageNum: currentPage, // 当前页面
                totalNum: pages, // 总页码
                totalList: total, // 记录总数量
                pageSize: pageSize,
                callback: function (num, size) { //回调函数
                    pageSize = size;
                    currentPage = num;
                    getBlogInfo(searchValue, num, size);
                    // 返回顶部
                    if ($(window).scrollTop() > 200) {
                        $('body,html').animate({
                            scrollTop: 0
                        });
                    }

                }
            });
        },
        error: function (err) {
            // 博客名为空
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }

    });
}

$("#searchBlog").click(function () {
    searchValue = $('#searchValue').val()
    getBlogInfo(searchValue, 1, pageSize);
    return false;
});

function getHotBlog() {
    $.ajax({
        type: "get",
        async: false,
        url: blogServer + "/blog/hotBlog?number=" + number,
        data: {},
        success: function (data) {
            console.log("热点博客数据 - 排名前 " + number + " 的博客");
            //清空数据
            $("#hotBlogs").empty();
            //返回的数据用data.rows获取内容
            $.each(data.datas, function (i, item) {
                if (item.blogContent === null) {
                    item.blogContent = "暂无内容";
                }
                $("#hotBlogs").append(
                    "<div class='sidebar-blog'>" +
                    "<a href='blog-details.html' class='image'>" +
                    "<img src='/images/blog/top-" + (i + 1) + ".png' alt=''>" +
                    "</a>" +
                    "<div class='content'>" +
                    "<h5><a onclick='blogDetail("+item.blogId+")' >" + item.blogTitle + "</a></h5>" +
                    "<h7><a onclick='blogDetail("+item.blogId+")' >" + item.blogContent.substring(0, 10) + " ……" + "</a></h7>" +
                    "<span style='color: gray; text-align: right; margin-top: 10px;'><i class='lnr lnr-eye'></i>&nbsp;&nbsp;<a href='#'>" + item.readNumber + "</a></span>" +
                    "<span style='color: gray; font-size: 10px; text-align: right; margin-top: 10px;'>" + item.createTime + "</span>" +
                    "</div>" +
                    "</div>"
                );
            });
        },
        error: function (err) {
            console.log(err);
        }
    });

}

function blogDetail(blogId){
    location.href ="/oa/blog/blogDetailsPage/"+blogId;

    $.ajax({
        type: "get",
        async: false,
        url: blogServer + "/blog/updateReadNumber/"+blogId,
        success: function (data) {
        },
        error: function (err) {
            console.log(err);
        }
    })
}
